package com.easy.user.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.common.bean.User;
import com.easy.common.service.IUserService;
import com.easy.user.mapper.UserMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@DubboService
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Override
    public User getUserById(Integer id) {
        return super.getById(id);
    }
    @Override
    public int addUser(User user) {
        // 设置默认角色为普通用户
        if (user.getRole() == null || user.getRole().isEmpty()) {
            user.setRole("普通用户");
        }
        // 如果是管理员角色，则设置为管理员
        if ("管理员".equals(user.getRole())) {
            user.setRole("管理员");
        }

        return super.save(user)?1:0;
    }
    @Override
    public int updateUser(User user) {
        return super.updateById(user)?1:0;
    }
    @Override
    public int deleteUser(Integer id) {
        User user = super.getById(id);
        if (user != null) {
            user.setIsdel(1);
            return super.updateById(user) ? 1 : 0;
        }
        return 0;
    }
    @Override
    public List<User> getAll(User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(user.getUsername() != null && !user.getUsername().isEmpty(), User::getUsername, user.getUsername());
        queryWrapper.like(user.getReal_name() != null && !user.getReal_name().isEmpty(), User::getReal_name, user.getReal_name());
        queryWrapper.eq(user.getGender() != null && !user.getGender().isEmpty(), User::getGender, user.getGender());
        // 添加role过滤条件
        queryWrapper.eq(user.getRole() != null && !user.getRole().isEmpty(), User::getRole, user.getRole());
        queryWrapper.eq(User::getIsdel, 0);
        return super.list(queryWrapper);
    }
    @Override
    public IPage<User> getPage(User user, IPage page) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(user.getUsername() != null && !user.getUsername().isEmpty(), User::getUsername, user.getUsername());
        queryWrapper.like(user.getReal_name() != null && !user.getReal_name().isEmpty(), User::getReal_name, user.getReal_name());
        queryWrapper.eq(user.getGender() != null && !user.getGender().isEmpty(), User::getGender, user.getGender());
        // 添加role过滤条件
        queryWrapper.eq(user.getRole() != null && !user.getRole().isEmpty(), User::getRole, user.getRole());
        queryWrapper.eq(User::getIsdel, 0);
        return super.page(page, queryWrapper);
    }
    // 在 UserServiceImpl 类中添加方法
    @Override
    public List<User> getDriverApplications(Integer status) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getDriver_apply_status, status);
        queryWrapper.eq(User::getIsdel, 0);
        return super.list(queryWrapper);
    }

    @Override
    public int approveDriverApplication(Integer id) {
        User user = super.getById(id);
        if (user != null) {
            // 更新司机申请状态为已通过
            user.setDriver_apply_status(2);
            // 更新用户角色为司机
            user.setRole("司机");
            return super.updateById(user) ? 1 : 0;
        }
        return 0;
    }

    @Override
    public int rejectDriverApplication(Integer id) {
        User user = super.getById(id);
        if (user != null) {
            // 更新司机申请状态为已拒绝
            user.setDriver_apply_status(3);
            return super.updateById(user) ? 1 : 0;
        }
        return 0;
    }

    @Override
    public User getByUsername(String username) {
        return super.getOne(new QueryWrapper<User>().eq("username", username));
    }

    @Override
    public User getByEmail(String email) {
        return super.getOne(new QueryWrapper<User>().eq("email", email));
    }

    @Override
    public boolean resetPasswordByEmail(String email, String newPassword) {
        return false;
    }

    @Override
    public boolean resetPassword(String username, String oldPassword, String newPassword) {
        return false;
    }
}
